package Notes;
/*
 * tetris.java
 *
 * Created on 2008?�� 1?�� 25?�� (�?), ?��?�� 4:57
 */



/**
 *
 * @author  seible
 */
import java.awt.*;
import java.math.*;


public class tetris extends javax.swing.JFrame {
    
    /** Creates new form tetris */
    public tetris() {
        initComponents();
    }
     
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tetris");
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        // TODO add your handling code here:
        int co;
        int i,j;
        co = evt.getKeyCode();
        if (co == 38){ // is up
            co=38;
            if (now_block == 0){
                this.rotate_zero();
                this.repaint();
            }else if (now_block == 1){
                this.rotate_one();
                this.repaint();
            }else if (now_block == 2){
                this.rotate_two();
                this.repaint(); 
            }else if (now_block == 3){
                this.rotate_three();
                this.repaint();
            }else if (now_block == 4){
                this.rotate_four();
                this.repaint();
                
            }
        }else if (co == 40){ // is down
            this.down_block();
            this.repaint();
        }else if (co == 37){ // is left
            if (this.can_move_left()){
                this.left_block();
                this.repaint();
            }
        }else if (co == 39){ // is right
            if (this.can_move_right()){
                this.right_block();
                this.repaint();
            }
        }else if (co==32){ // is space bar
            end_down_flag=0;
            for (;;){
                this.down_block();
                this.repaint();
                if (end_down_flag==1) break;
            }
        }
        
    }//GEN-LAST:event_formKeyPressed
    
    /**
     * @param args the command line arguments
     */
    public int map_x=12,map_y=20;
    public int block[][] = new int[map_y+1][map_x+1];
    public int now_block, now_block_state;
    public int end_down_flag,end_game_flag;
    public void paint(Graphics g){
        g.clearRect(0, 0, map_x*10+10, map_y*10+30);
        int i,j;
        for (i=0;i<map_y;i++){
            for (j=0;j<map_x;j++){
                if (block[i][j]!=0){
                    g.drawRect(j*10+10,i*10  +30,10,10);
                }
            }
        }
    }
    public boolean can_move_right()    {
        int i,j,bflag;
        bflag=0;
        for (i=0;i<map_y;i++){ 
            for (j=0;j<map_x;j++){
                if (block[i][j] == 2){
                    if (j == map_x-1){
                        bflag=1;
                        break;
                    }
                    if (block[i][j+1] == 1){
                        bflag=1;
                        break;
                    }
                }
            }
            if (bflag==1) break;
        }
        if (bflag==0){
            return true;
        }else{
            return false;
        }
    }
    public boolean can_move_left(){
        int i,j,bflag;
        bflag=0;
        for (i=0;i<map_y;i++){
            for (j=0;j<map_x;j++){
                if (block[i][j] == 2){
                    if (j == 0){
                        bflag=1;
                        break;
                    }
                    if (block[i][j-1] == 1){
                        bflag=1;
                        break;
                    }
                }
            }
            if (bflag==1) break;
        }
        if (bflag==0){
            return true;
        }else{
            return false;
        }
    }
    public void rotate_zero(){
        int i,j;
        for (i=map_y-1;i>=0;i--){
            for (j=map_x-1;j>=0;j--){
                if (block[i][j] == 2 && now_block_state == 0){
                    if (j>=3){
                        if (block[i][j-1] == 0 && block[i][j-2] == 0 && block[i][j-3] == 0){
                            block[i][j-1] = block[i][j-2] = block[i][j-3] = 2;
                            block[i-1][j] = block[i-2][j] = block[i-3][j] = 0;
                            now_block_state = 1;
                        }
                    }
                    return;
                }else if (block[i][j] == 2 && now_block_state == 1){
                    if (block[i][j] == 2 && now_block_state == 1){
                        if (block[i-1][j] == 0 && block[i-2][j] == 0 && block[i-3][j] == 0){
                            block[i-1][j] = block[i-2][j] = block[i-3][j] = 2;
                            block[i][j-1] = block[i][j-2] = block[i][j-3] = 0;
                            now_block_state = 0;
                        }
                    }
                    return;
                }
            }
        }
    }
    public void rotate_one(){
        
        int i,j;
        for (i=map_y-1;i>=0;i--){
            for (j=0;j<map_x;j++){
                if (block[i][j] == 2 && now_block_state == 0){
                    if (j>=1 && j< map_x-1){
                        if (block[i][j-1] == 0 && block[i-1][j-1] == 0 && block[i-2][j-1] == 0 && block[i-1][j+1] == 0 && block[i-2][j+1] == 0){
                            block[i][j-1] = block[i-1][j+1] = 2;
                            block[i-2][j] = block[i-1][j] = 0;
                            now_block_state = 1;
                        }
                    }
                    return;
                }else if (block[i][j] == 2 && now_block_state == 1){
                    if (i != map_y-1 && j< map_x-2){
                        if (block[i-1][j] == 0 && block[i-1][j+1] == 0 && block[i+1][j] == 0 && block[i+1][j+1] == 0 && block[i+1][j+2] == 0){
                            block[i-1][j+1] = block[i+1][j+2] = 2;
                            block[i][j] = block[i][j+1] = 0;
                            now_block_state = 2;
                        }
                    }
                    return;
                }else if (block[i][j] == 2 && now_block_state == 2){
        /*
         *            
         *          *         * *           * * *
         * *    * * *           *           *
                                *
         */           
                    if (j < map_x-1){
                        if (block[i][j-1] == 0 && block[i-1][j-1] == 0 && block[i-1][j+1] == 0 && block[i-2][j+1] == 0){
                            block[i-1][j-1] = block[i-2][j+1] = 2;
                            block[i][j] = block[i-1][j] = 0;
                            now_block_state = 3;
                        }
                    }
                    return;
                }else if (block[i][j] == 2 && now_block_state == 3){
                    if (j < map_x-2){
                        if (block[i][j+1] == 0 && block[i-2][j] == 0 && block[i-2][j+1] == 0){
                         block[i][j+1] = block[i-2][j] = 2;
                          block[i-1][j+1] = block[i-1][j+2] = 0;
                          now_block_state = 0;
                        }
                    }
                    return;
                }
            }
        }
    }
    public void rotate_two(){

        int i,j;
        for (i=map_y-1;i>=0;i--){
            for (j=0;j<map_x;j++){
                if (block[i][j] == 2 && now_block_state == 0){
                    if (j < map_x-2){
                        if (block[i-1][j] == 0 && block[i][j+2] == 0 && block[i-1][j+2] == 0){
                            block[i-1][j] = block[i][j+2] = 2;
                            block[i-1][j+1] = block[i-2][j+1] = 0;
                            now_block_state = 1;
                        }
                    }
                    return;
                }else if (block[i][j] == 2 && now_block_state == 1){
                    if (i != map_y-1){
                        if (block[i-1][j+1] == 0 && block[i+1][j] == 0 && block[i+1][j+1] == 0){
                            block[i-1][j+1] = block[i+1][j] = 2;
                            block[i][j+1] = block[i][j+2] = 0;
                            now_block_state = 2;
                        }
                    }
                    return;
                }else if (block[i][j] == 2 && now_block_state == 2){
        /*
           * 
           *   *          * *       * * *   
         * *   * * *      *             *
                          * 
         */
                    if (j!=0){
                        if (block[i-2][j-1] == 0 && block[i-1][j-1] == 0 && block[i-1][j+1] == 0 && block[i][j+1] == 0){
                            block[i-2][j-1] = block[i-1][j+1] = 2;
                            block[i][j] = block[i-1][j] = 0;
                            now_block_state = 3;
                        }
                    }
                    return;
                }else if (block[i][j] == 2 && now_block_state == 3){
                    if (block[i][j-1] == 0 && block[i][j-2] == 0 && block[i-2][j] == 0 && block[i-2][j-1] == 0 && block[i-2][j-2] == 0){
                        block[i][j-1] = block[i-2][j] = 2;
                        block[i-1][j-1] = block[i-1][j-2] = 0;
                        now_block_state = 0;
                    }
                    return;
                }
            }
        }
    }
    public void rotate_three(){
    /*
       * 
     * *     * *
     *         * *
     */
        int i,j;
        int k,l;
        for (i=map_y-1;i>=0;i--){
            for (j=0;j<map_x;j++){
                if (block[i][j] == 2 && now_block_state == 0){
                    if (j>0){
                        if (block[i][j-1] == 0 && block[i-1][j-1] == 0 && block[i][j+1] == 0){
                            block[i-1][j-1] = block[i][j+1] = 2;
                            block[i-1][j+1] = block[i-2][j+1] = 0;
                            now_block_state = 1;
                        }
                    }
                    return;
                }else if (block[i][j] == 2 && now_block_state == 1){
                    if (block[i-1][j+1] == 0 && block[i-2][j] == 0 && block[i-2][j+1] == 0){
                        block[i-1][j+1] = block[i-2][j+1] = 2;
                        block[i-1][j-1] = block[i][j+1] = 0;
                        now_block_state = 0;
                    }
                    return;
                }
            }
        }
    }
    public void rotate_four(){
    /*
     *
     * *          * * 
       *        * *
     */
        int i,j;
        int k,l;
        for (i=map_y-1;i>=0;i--){
            for (j=0;j<map_x;j++){
                if (block[i][j] == 2 && now_block_state == 0){
                    if (j < map_x-1){
                        if (block[i][j-1] == 0 && block[i][j+1] == 0 && block[i-1][j+1] == 0){
                            System.out.println("Shut"+j+","+i);
                            block[i][j-1] = block[i-1][j+1] = 2;
                            block[i-1][j-1] = block[i-2][j-1] = 0;
                            now_block_state = 1;
                        }
                    }
                    return;
                }else if (block[i][j] == 2 && now_block_state == 1){
                    if (block[i-1][j] == 0 && block[i-2][j] == 0){
                        block[i-1][j] = block[i-2][j] = 2;
                        block[i][j] = block[i-1][j+2] = 0;
                        now_block_state = 0;
                    }
                    return;
                }
            }
        }
    }
    public void create_block(){
        int sel = (int)(Math.random()*6);
        int eflag;
        eflag=0;
        now_block = sel;
        now_block_state = 0;
        if (sel == 0){
            if (block[0][6] == 0 && block[1][6] == 0 && block[2][6] == 0 && block[3][6] == 0){
                block[0][6]=block[1][6]=block[2][6]=block[3][6] = 2;
            }else{
                eflag=1;
            }
        }else if (sel==1){
            if (block[0][5] == 0 && block[0][6] == 0 && block[1][5] == 0 && block[1][6] == 0 && block[2][5] == 0 && block[2][6] == 0){
                block[0][5]=block[1][5]=block[2][5]=block[2][6] = 2;
            }else{
                eflag=1;
            }
        }else if (sel==2){
            if (block[0][5] == 0 && block[0][6] == 0 && block[1][5] == 0 && block[1][6] == 0 && block[2][5] == 0 && block[2][6] == 0){
                block[0][6]=block[1][6]=block[2][6]=block[2][5] = 2;
            }else{
                eflag=1;
            }
        }else if (sel==3){
            if (block[0][5] == 0 && block[0][6] == 0 && block[1][5] == 0 && block[1][6] == 0 && block[2][5] == 0 && block[2][6] == 0){
                block[0][6]=block[1][6]=block[1][5]=block[2][5] = 2;
            }else{
                eflag=1;
            }
        }else if (sel==4){
            if (block[0][5] == 0 && block[0][6] == 0 && block[1][5] == 0 && block[1][6] == 0 && block[2][5] == 0 && block[2][6] == 0){
                block[0][5]=block[1][5]=block[1][6]=block[2][6]= 2;
            }else{
                eflag=1;
            }
        }else if (sel==5){
            if (block[0][5] == 0 && block[0][6] == 0 && block[1][5] == 0 && block[1][6] == 0){
                block[0][5]=block[0][6]=block[1][5]=block[1][6] = 2;
            }else{
                eflag=1;
            }
        }
        if (eflag==1){ // game over
            System.out.println("GAME OVER");
            end_game_flag=1;
        }
    }
    public void right_block(){
        int i,j;
        for (i=0;i<map_y;i++){
            for (j=map_x-1;j>=0;j--){
                if (block[i][j] == 2){
                    block[i][j+1] = 2;
                    block[i][j] = 0;
                }
            }
        }
    }
    public void left_block(){
        int i,j;
        for (i=0;i<map_y;i++){
            for (j=0;j<map_x;j++){
                if (block[i][j] == 2){
                    block[i][j-1] = 2;
                    block[i][j] = 0;
                }
            }
        }        
    }
    public void down_block() {
        int i,j,flag,count;
        flag=0;
        for (i=0;i<map_y;i++){
            for (j=0;j<map_x;j++){
                if (block[i][j] == 2){
                    if (i+1 == map_y){
                        flag = 2;
                        break;
                    }
                    if (block[i+1][j] == 1){
                        flag = 1;
                    }
                }
            }
            if (flag==2){
                break;
            }
        }
        if (flag==0){
            for (i=map_y-1;i>=0;i--){
                for (j=0;j<map_x;j++){
                    if (block[i][j] == 2){
                        block[i+1][j] = 2;
                        block[i][j]=0;
                    }
                }
            }
        }else if (flag==2 || flag == 1){
            for (i=0;i<map_y;i++){
                for (j=0;j<map_x;j++){
                    if (block[i][j] == 2) block[i][j] = 1;
                }
            }
            this.clear_line();
            this.create_block();
            end_down_flag=1;
        }
    }
    public void clear_line(){
        int i,j,k,cnt;
        for (i=map_y-1;i>=0;i--){
            cnt=0;
            for (j=0;j<map_x;j++){
                if (block[i][j] == 1){
                    cnt++;
                }
            }
            if (cnt == map_x){
                for (k=i;k>=1;k--){
                    for (j=0;j<map_x;j++){
                        block[k][j] = block[k-1][j];
                    }
                }
                i++;
            }
        }
    }
    public static void main(String args[]){
        long prev,now;
        tetris a = new tetris();
        a.end_game_flag=0;
        a.setVisible(true);
        a.setSize(134  , 235);
        a.create_block();
        a.repaint();
        prev = System.currentTimeMillis();
        for (;;){
            now = System.currentTimeMillis();
            if (now-prev >=1000){
                a.repaint();
                a.down_block();
                if (a.end_game_flag==1){
                    System.exit(0);
                    break;                    
                }
                prev = System.currentTimeMillis();
            }
        }
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
}
